//
//  main.swift
//  heap
//
//  Created by lhc on 2024/8/8.
//

import Foundation

func testMaxHeap() throws {
    let heap = MaxHeap<Int>()
    let range = 0..<10000
    for _ in range {
        heap.add(element: Int.random(in: range))
    }
    
    var retList = [Int]()
    for _ in range {
        retList.append(try heap.extractMax())
    }
    
    for i in 1..<10000 {
        if retList[i] > retList[i - 1] {
            print("实现的最大堆有问题，每次吐出的最大值并不是最大")
        }
    }
    print("实现的最大堆没有问题")
}

func testReplace() throws {
    let heap = MaxHeap<Int>()
    heap.add(element: 5)
    heap.add(element: 6)
    heap.add(element: 4)
    heap.add(element: 7)
    // 7 6 4 5
    
    let _ = try heap.replace(element: 1)
    // 6 5 4 1
    if heap.description == "\([6,5,4,1])" {
        print("最大堆的replace方法实现没有问题")
    }else {
        print("最大堆的replace方法实现有fatal问题")
    }
}

func testHeapify() throws {
    let array = [1, 2, 3, 4, 5]
    let heap = try MaxHeap(array)
    if heap.description == "\([5,4,3,1,2])" {
        print("heapify方法没有问题")
    }else {
        print("heapify方法有fatal问题")
    }
}


do {
    try testMaxHeap()
}catch let error {
    print(error)
}

do {
    try testReplace()
}catch let error {
    print(error)
}

do {
    try testHeapify()
}catch let error {
    print(error)
}


